package com.arpa.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.enums.EmploymentTypeEnum;
import com.arpa.wms.common.util.DateUtil;
import com.arpa.wms.domain.dto.PackBillingMethodDTO;
import com.arpa.wms.domain.entity.PackBillingMethod;
import com.arpa.wms.domain.vo.PackBillingMethodVO;
import com.arpa.wms.mapper.PackBillingMethodMapper;
import com.arpa.wms.service.IPackBillingMethodService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 打包计费方式 服务实现类
 * </p>
 *
 * @author rfwang
 * @since 2021-06-29
 */
@Service
@Log4j2
public class PackBillingMethodServiceImpl extends ServiceImpl<PackBillingMethodMapper, PackBillingMethod> implements IPackBillingMethodService {

    @Autowired
    private PartyCache partyCache;

    /**
     * 根据仓库、作业人、作业时间获取打包计费信息
     * @param dto
     * @return
     */
    @Override
    public PackBillingMethodVO getPackBillingInfo(PackBillingMethodDTO dto) {
        dto.setJobTime(DateUtil.getYearMonthDay());
        PackBillingMethodVO packBillingMethodVO = new PackBillingMethodVO();
        PackBillingMethod packBillingMethod = this.getOne(new LambdaQueryWrapper<PackBillingMethod>()
                .eq(PackBillingMethod::getWarehouseCode, dto.getWarehouseCode())
                .eq(PackBillingMethod::getGroupCode,UserUtil.getBranchCode())
                .eq(PackBillingMethod::getPartyCode, dto.getPartyCode())
                .eq(PackBillingMethod::getJobTime, dto.getJobTime())
        .eq(PackBillingMethod::getGroupCode,UserUtil.getBranchCode()));
        if (null == packBillingMethod) {
            return null;
        }
        BeanUtil.copyProperties(packBillingMethod,packBillingMethodVO,false);
        return packBillingMethodVO;
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(PackBillingMethod entity) {
        if (StringUtils.isBlank(entity.getCode())) {
            entity.setCode(IdUtil.simpleUUID());
        }
        entity.setGroupCode(UserUtil.getBranchCode());
        if (StringUtils.isBlank(entity.getCreatedBy())) {
            entity.setCreatedBy(UserUtil.getCode());
            entity.setCreatedName(partyCache.translate(UserUtil.getCode()));
        }
        return super.save(entity);
    }

    /**
     * 查询列表
     * @param packBillingMethodDTO
     * @return
     */
    @Override
    public List<PackBillingMethodVO> queryList(PackBillingMethodDTO packBillingMethodDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StrUtil.isNotEmpty(packBillingMethodDTO.getSortField())){
            packBillingMethodDTO.setSortField(CommonUtil.camel2Underline(packBillingMethodDTO.getSortField()));
        }
        List<PackBillingMethodVO> packBillingMethodVOS = this.baseMapper.queryList(packBillingMethodDTO);
        return packBillingMethodVOS.stream().peek(info -> {
            if (StrUtil.isNotEmpty(info.getEmploymentType())) {
                info.setEmploymentTypeText(EmploymentTypeEnum.translate(info.getEmploymentType()));
            }
        }).collect(Collectors.toList());
    }

    /**
     * 查询合计,包含总数
     * @param packBillingMethodDTO
     * @return
     */
    @Override
    public PackBillingMethodVO queryListSum(PackBillingMethodDTO packBillingMethodDTO){
        return this.baseMapper.queryListSum(packBillingMethodDTO);
    }

}
